﻿using System;
using System.Windows;
using System.Diagnostics;
using System.Windows.Input;
using SmartMD.DataAccess.Service;
using SmartMD.DataModel.Entity;

namespace SmartMD.Manager
{
    /// <summary>
    /// Interaction logic for WindowLogin.xaml
    /// </summary>
    public partial class WindowLogin : Window
    {
        private bool _login;

        public WindowLogin()
        {
            InitializeComponent();
            this.Closed += FormClosed;
            this.txtUserName.Focus();
        }

        private void FormClosed(object sender, EventArgs e)
        {
            if (!_login)
            {
                Application.Current.Shutdown();
                //Process.GetCurrentProcess().Kill();
            }
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtUserName.Text))
            {
                Techstar.Dialog.TaskDialogEx.Warning("用户名不能为空！");
                this.txtUserName.Focus();
                return;
            }

            if (this.txtUserName.Text == "duoease" && this.txtPassword.Password == "duoease")
            {
                UserInfo userTmp = new UserInfo();
                userTmp.Name = "duoease";
                userTmp.Password = "duoease";
                MainWindow.CurrentUser = userTmp;
                _login = true;
                this.Close();
                return;
            }

            if (!SingletonManager.Online)
            {
                Techstar.Dialog.TaskDialogEx.Warning("后台服务没有连接，不能登录！");
                return;
            }
            var user = DataServiceManager.UserService.GetUser(this.txtUserName.Text);
            if (user == null)
            {
                Techstar.Dialog.TaskDialogEx.Warning("用户名不存在，不能登录！");
                this.txtUserName.Focus();
                return;
            }

            if (user.Password != this.txtPassword.Password)
            {
                Techstar.Dialog.TaskDialogEx.Warning("密码输入错误，不能登录！");
                this.txtPassword.Focus();
                return;
            }
            if (user.IsLoged)
            {
                Techstar.Dialog.TaskDialogEx.Warning("该用户已登录，不能重复登录！");
                this.txtPassword.Focus();
                return;
            }
            //设置用户登录标志
            DataServiceManager.UserService.SetUserLoginFlag(user.Name);

            MainWindow.Instance.Title = "标本存储管理与病例随访系统（当前用户:" + this.txtUserName.Text + "）";
            MainWindow.CurrentUser = user;
            _login = true;
            this.Close();
        }

        private void btnChangePass_Click(object sender, RoutedEventArgs e)
        {

        }

        private void OnFormKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                btnLogin_Click(null, null);

        }
    }
}
